package com.example.musicplayer01;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import com.example.adapters.MusicAdapterStyleOne;
import com.example.entities.MP3;
import com.example.musicplayer01.databinding.ActivityMainBinding;
import com.example.services.MusicService;
import com.example.utils.MusicUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    private ActivityMainBinding binding;
    private MusicAdapterStyleOne adapter;
    private LinearLayoutManager manager;
    public static Intent service;
    private Thread musicProgress;
    public static Handler handler;
    private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView(){
        service=new Intent(this, MusicService.class);
        handler=new Handler(Looper.getMainLooper()){
            @SuppressLint("SetTextI18n")
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case 100:
                        binding.seekBar.setProgress(msg.arg1);
                        String str1=MusicUtils.timeParse(msg.arg1);
                        duration=MusicService.data.get(MusicService.index).getDuration();
                        String str2=MusicUtils.timeParse(duration);
                        binding.musicDuration.setText(str1+"/"+str2);
                        break;
                    case 200:
                        reloadMusic();
                        break;
                }
            }
        };
        changeButton();
        loadData();
        if(MusicService.data==null || MusicService.data.size()==0){
            Toast.makeText(this, "/Storage/emulated/0/Music 里没有音频数据，请先添加好，再运行", Toast.LENGTH_LONG).show();
        }else{
            initRecyclerView();
            initListeners();
            initTextView();
            binding.seekBar.setOnSeekBarChangeListener(this);
        }
    }

    public void initSeekBar(){
        binding.seekBar.setProgress(0);
        duration=MusicService.data.get(MusicService.index).getDuration();
        binding.seekBar.setMax(duration);
    }

    @SuppressLint("SetTextI18n")
    private void initTextView(){
        binding.musicTitle.setText(MusicService.data.get(MusicService.index).getTitle());
        binding.musicDuration.setText("00:00/"+MusicUtils.timeParse(MusicService.data.get(MusicService.index).getDuration()));
    }

    private void loadData(){
        MusicService.data= MusicUtils.loadLocalMP3(this);
    }

    private void initRecyclerView(){
        adapter=new MusicAdapterStyleOne(MusicService.data);
        binding.recyclerView.setAdapter(adapter);
        manager=new LinearLayoutManager(this);
        binding.recyclerView.setLayoutManager(manager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pre:
                MusicService.index--;
                musicChange();
                break;
            case R.id.next:
                MusicService.index++;
                musicChange();
                break;
            case R.id.play:
                MusicService.musicSwitch=!MusicService.musicSwitch;
                changeButton();
                if(MusicService.mediaPlayer!=null) MusicService.action="playOrPause";
                initSeekBar();
                startService(service);
                break;
            case R.id.stop:
                MusicService.musicSwitch=false;
                changeButton();
                stopService(service);
                break;
        }
    }

    private void changeButton(){
        if(MusicService.musicSwitch){
            binding.play.setImageResource(R.drawable.ic_baseline_motion_photos_paused_24);
        }else{
            binding.play.setImageResource(R.drawable.ic_baseline_not_started_24);
        }
    }

    private void initListeners(){
        binding.play.setOnClickListener(this);
        binding.stop.setOnClickListener(this);
        binding.next.setOnClickListener(this);
        binding.pre.setOnClickListener(this);
    }

    private void musicChange(){
        if(MusicService.index<0) MusicService.index=MusicService.data.size()-1;
        if(MusicService.index>=MusicService.data.size()) MusicService.index=0;
        reloadMusic();
    }

    public void reloadMusic(){//刷新音乐
        MusicService.action="changeMusic";//操作识别
        initSeekBar();
        startService(service);
        MusicService.musicSwitch=true;
        changeButton();
        initTextView();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        MusicService.progress=progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        MusicService.musicSwitch=false;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        MusicService.mediaPlayer.seekTo(MusicService.progress);
        MusicService.musicSwitch=true;
    }
}